package com.sunday.common.mybatis.plus.query;

import com.google.gson.JsonObject;
import com.sunday.common.core.lambda.toolkit.support.SFunction;
import com.sunday.common.mybatis.plus.core.MybatisPlusFieldMapping;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Arrays;
import java.util.List;

/**
 * 排序 Wrapper
 *
 * @author zsy
 * @since 2022/10/27
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class OrderItemWrapper<T> {

    /**
     * 需要进行排序的字段
     */
    private SFunction<T, ?> column;
    /**
     * 是否正序排列，默认 true
     */
    private boolean asc = true;

    public static <T> OrderItemWrapper<T> asc(SFunction<T, ?> column) {
        return build(column, true);
    }

    public static <T> OrderItemWrapper<T> desc(SFunction<T, ?> column) {
        return build(column, false);
    }

    private static <T> OrderItemWrapper<T> build(SFunction<T, ?> column, boolean asc) {
        return new OrderItemWrapper(column, asc);
    }

    public static <T> List<OrderItemWrapper<T>> ascs(SFunction<T, ?>... columns) {
        return Arrays.stream(columns).map(OrderItemWrapper::asc).toList();
    }

    public static <T> List<OrderItemWrapper<T>> descs(SFunction<T, ?>... columns) {
        return Arrays.stream(columns).map(OrderItemWrapper::desc).toList();
    }

    @Override
    public String toString() {
        JsonObject json = new JsonObject();
        json.addProperty("column", MybatisPlusFieldMapping.getColumnName(column));
        json.addProperty("asc", asc);
        return json.toString();
    }

}
